package com.make.house.api.utils;


import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by xujun on 2017/11/18.
 * java 实现的3des 加密算法
 */

public class ThreeDesUtil {

    /**
     * 加密String明文输入,String密文输出
     * @param strMing String明文
     * @return String密文
     */
    public static String getEncString(String strMing) {
        byte[] byteMi;
        byte[] byteMing;
        String strMi = "";
        Base64 base64en = new Base64();
        try {
            byteMing = strMing.getBytes("UTF8");
            byteMi = getEncCode(byteMing);
            strMi = base64en.encode(byteMi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMi;
    }

    /**
     * 解密 以String密文输入,String明文输出
     * @param strMi String密文
     * @return String明文
     */
    public static String getDesString(String strMi) {
        Base64 base64De = new Base64();
        byte[] byteMing;
        byte[] byteMi;
        String strMing = "";
        try {
            byteMi = base64De.decode(strMi);
            byteMing = getDesCode(byteMi);
            strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMing;
    }

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     * @param byteS byte[]明文
     * @return byte[]密文
     */
    private static byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
//            DESKeySpec dks = new DESKeySpec(strKey.getBytes());
//            Key secretKey = SecretKeyFactory.getInstance("DES").generateSecret(dks);
//            IvParameterSpec paramSpec = new IvParameterSpec(iv);
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }



    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     * @param byteD byte[]密文
     * @return byte[]明文
     */
    private static byte[] getDesCode(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
//            IvParameterSpec zeroIv = new IvParameterSpec(iv);
//            SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "DES");
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }



    //定义加密算法，有DES、DESede(即3DES)、Blowfish
    private static final String Algorithm = "DESede";
    //因为只能取24位，所以讲secret的前24位截取
    private static final String PASSWORD_CRYPT_KEY = "27921b457e1b58ccf09f0b31";

    /*
     * 根据字符串生成密钥字节数组
     * @param keyStr 密钥字符串
     * @return
     * @throws UnsupportedEncodingException
     */
    public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
        byte[] key = new byte[24];    //声明一个24位的字节数组，默认里面都是0
        byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
        /*
         * 执行数组拷贝
         * System.arraycopy(源数组，从源数组哪里开始拷贝，目标数组，拷贝多少位)
         */
        if(key.length > temp.length){
            //如果temp不够24位，则拷贝temp数组整个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, temp.length);
        }else{
            //如果temp大于24位，则拷贝temp数组24个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    }

}
